package com.pt.controller;

import com.pt.common.ApiResponse;
import com.pt.service.TorrentService;
import com.pt.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/public")
@RequiredArgsConstructor
@Api(tags = "公共接口")
public class PublicController {
    
    private final UserService userService;
    private final TorrentService torrentService;
    
    @GetMapping("/stats")
    @ApiOperation("获取公共统计信息")
    public ApiResponse<Map<String, Object>> getPublicStats() {
        System.out.println("=== PublicController.getPublicStats被调用 ===");
        
        Map<String, Object> stats = new HashMap<>();
        
        try {
            // 只提供基本的公开统计信息
            long totalUsers = userService.count();
            long totalTorrents = torrentService.count();
            
            System.out.println("总用户数: " + totalUsers);
            System.out.println("总种子数: " + totalTorrents);
            
            stats.put("totalUsers", totalUsers);
            stats.put("totalTorrents", totalTorrents);
            
            System.out.println("返回的stats: " + stats);
            ApiResponse<Map<String, Object>> response = ApiResponse.success(stats);
            System.out.println("最终响应: " + response);
            
            return response;
        } catch (Exception e) {
            System.out.println("PublicController.getPublicStats发生异常: " + e.getMessage());
            e.printStackTrace();
            return ApiResponse.error("获取统计信息失败: " + e.getMessage());
        }
    }
} 